package com.slkj.shilixiaoyuanapp.net.callback;

import android.content.Context;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.slkj.shilixiaoyuanapp.net.CommonResult;
import com.slkj.shilixiaoyuanapp.net.HttpConfig;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.lang.ref.WeakReference;
import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created  on 2018-08-15
 *
 * @author 唐开阔
 * @describe
 * 文件下载监听
 */
public abstract class DownFileCallBack<T> implements Observer<CommonResult<T>> {
    private boolean isShowDialog;
    private Context context;
    private String mes = "加载中请稍后";
    StateLayout stateLayout;
    QMUITipDialog dialog;

    public DownFileCallBack() {
        this(false, null);
    }

    public DownFileCallBack(Context context) {
        this(false,context);
    }
    public DownFileCallBack(boolean isShowDialog, Context context) {
        this(isShowDialog,context,"加载中请稍后");
    }

    public DownFileCallBack(boolean isShowDialog, Context context, String mes) {
        this.isShowDialog = isShowDialog;
        this.context = context;
        this.mes = mes;
        if (isShowDialog) {
            WeakReference<Context> sr = new WeakReference<Context>(context);
            dialog = new QMUITipDialog.Builder(context)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord(mes)
                    .create(false);
        }
    }

    public DownFileCallBack(StateLayout stateLayout) {
        this.stateLayout = stateLayout;
    }

    @Override
    public void onSubscribe(Disposable d) {
        if (isShowDialog) {
            dialog.show();
        }
    }

    @Override
    public void onNext(CommonResult<T> tCommonResult) {
        if (HttpConfig.RESPONSE_OK.equals(tCommonResult.getStatus())) {
            if (stateLayout != null) {
                stateLayout.showContentView();
            }
            onCallBackSuccess(tCommonResult.getData());
        } else {
            onFail(tCommonResult.getErrorMsg());
        }
    }

    @Override
    public void onError(Throwable e) {
        if (isShowDialog) {
            dialog.dismiss();
            dialog.cancel();
        }
        if (stateLayout != null) {
            stateLayout.showErrorView();
        }
        if (e instanceof SocketTimeoutException) {
            onFail("链接超时...请稍后再试");

        } else if (e instanceof ConnectException) {
            onFail("服务器连接失败...");

        } else {
            onFail(e.getMessage());
        }
    }

    @Override
    public void onComplete() {
        if (isShowDialog) {
            dialog.dismiss();
        }
    }

    /**
     * 数据请求成功
     *
     * @param data
     */
    public abstract void onCallBackSuccess(T data);

    /**
     * 数据请求失败
     *
     * @param mes
     */
    public void onFail(String mes) {
        if (context != null) {
            Toast.makeText(context, mes, Toast.LENGTH_SHORT).show();
        }
        if (stateLayout != null) {
            stateLayout.showErrorView();
        }
    }

    ;
}
